// pages/tabBar/enterprise/enterprise.js
import { request, upRequest, wxuuid } from "../../../../../utils/util.js"
const { $Message } = require('../../../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],     //文件列表
    isLoading: true,    //加载动画
    classifyType: "user", //类型显示
    visible1: false, //加号显示
    actions1: [      //加号内容
      {
        name: '通过粘贴板链接保存文章',
        icon: 'accessory',
      },

    ],
    visibleUrl: false, //modal的显示
    urlValue: "", //保存url
    current:1,   //默认第一页
    pageSize:15 //默认条数
  },

  /**
   * 页面初始加载的时候调用
   */
  onLoad: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setStorageSync('location', "user")
    this.getFileList();
  },

  /**
   * 上啦加载
   */
  onReachBottom:function () {
    const current = this.data.current + 1
    const that = this
    const param = {
      current,
      pageSize:that.data.pageSize,
      is_gzh:true
    }
    request("GET","/api/service/docinfo/",param,"",true)
    .then( res =>{
        if(res.statusCode === 200){
          let newList = JSON.stringify(res.data.data) === '{}' ? that.data.fileList : that.data.fileList.concat(res.data.data.data)
          let newCurrent = JSON.stringify(res.data.data) === '{}' ? current - 1 : current
          that.setData({
            ...that.data,
            fileList:newList,
            current:newCurrent
          })
        }
    })
},
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this
    const param = {
      is_gzh: true,
      pageSize:that.data.pageSize,
      current:1,
    }
    request("GET", "/api/service/docinfo/", param).then(res => {
      if(res.statusCode === 200){
        that.setData({
          ...that.data,
          fileList: res.data.data.data,
          current:1
        })
        wx.stopPullDownRefresh()
      }
      
    })


  },


  /**
   * 获取数据的封装
   */
  getFileList() {

    const that = this
    const param = {
      is_gzh: true,
      pageSize:that.data.pageSize,
      current:1
    }
    request("GET", "/api/service/docinfo/", param).then(res => {
      that.setData({
        fileList: res.data.data.data,
        isLoading: false
      })

    })
  },

  /**
   * 打开上啦菜单
   */
  add() {
    this.setData({
      visible1: true
    });
  },

  /**
  * 关闭上啦菜单
  */
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  /**
     * 上啦框里的各个事件
     * @param {}} param0 
     */
  handleClickItem1({ detail }) {
    const that = this
    wx.getClipboardData({
      success(res) {
        if ((/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/).test(res.data)) {
          that.setData({
            ...that.data,
            urlValue: res.data,
            visibleUrl: true
          })
  

        } else {
          wx.showToast({
            title: '剪贴板为空或不是有效的URL',
            icon: "none",
            duration: 1000
          })
        }


      },


    })
  },

  /**
   * 确定保存文章
   */
  handleUrlOk() {
    const that = this
    that.setData({
      ...that.data,
      visibleUrl:false,
    })
    const param = {
      url: this.data.urlValue
    }
    request("POST", "/api/upload/gzhtopdf/", param, "", true, "提取中,请稍等")
      .then(res => {
        if (res.statusCode === 200) {
          that.setData({
            ...that.data,
          
            visible1:false
          })
          that.getFileList();
          $Message({
            content: res.data.msg,
            type: 'success'
          })
        }else if(res.statusCode === 432){
          that.setData({
            ...that.data,
          
            visible1:false
          })
          that.getFileList();
          $Message({
            content: res.data.msg,
            type: 'warning'
          })
          
        }
      })
  },
  /**
   * 取消事件
   */
  handleUrlCancel() {
    this.setData({
      ...this.data,
      visibleUrl: false
    })
  },
    /**
   * 多选删除
   * @param {*} e 
   */
  multipleChoice(e){
    this.selectComponent("#halfScreen").showHalfScreen()
  }

})